/*
 * JniFileMethod.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.jni;

import java.io.IOException;
import java.io.PrintWriter;

import com.thoughtworks.qdox.model.JavaMethod;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeFileText;
import com.cute4j.tools.ng.Utils;

/**
 * JNI File Method.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public abstract class JniFileMethod extends NativeFileText { // NOPMD

// Private Fields. -------------------------------------------------------------

  private final JavaMethod method;
  private final boolean overridden;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new JniFileMethod object.
   *
   * @param nativeFile the native file
   * @param method the java method
   * @param overridden <tt>true</tt> if the method is overridden
   */
  public JniFileMethod(AbstractNativeFile nativeFile, JavaMethod method,
                       boolean overridden) {
    super(nativeFile);
    if (method == null) {
      throw new IllegalArgumentException("Null java method");
    }
    this.method = method;
    this.overridden = overridden;
  }

// Protected Methods. ----------------------------------------------------------

  /**
   * Returns parameters of the method.
   *
   * @param javaMethod the method
   *
   * @return parameters of the method
   */
  protected abstract String[] getMethodParameters(JavaMethod javaMethod);

  /**
   * Stores body of the method.
   *
   * @param pw the print writer
   */
  protected abstract void storeBody(PrintWriter pw);

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void storeContent(PrintWriter pw) throws IOException {
    pw.print("JNIEXPORT ");
    pw.print(Utils.getJniType(method.getReturns()));
    pw.println(" JNICALL");
    pw.println(Utils.getJniMethodName(method, overridden));
    pw.print("  (");
    String[] args = getMethodParameters(method);
    for (int i = 0; i < args.length; i++) {
      if (i > 0) {
        pw.print(", ");
      }
      pw.print(args[i]);
    }
    pw.print(")");
    storeBody(pw);
    pw.println();
  }

}
